package com.noble.library.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Looper;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewManager;
import android.widget.ImageView;

import com.noble.library.basic.log.PLogger;
import com.noble.library.observer.MsgMgr;

/**
 * view操作工具类
 */
public class ViewUtils {
    /**
     * 获取控件的高度
     */
    public static int getViewHeight(View view) {
        if (null == view) return 0;
        view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        return view.getMeasuredHeight();
    }

    /**
     * 设置背景bitmap： 兼容低版本
     */
    public static void setBackground(View v, Drawable background) {
        if (v == null) return;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            v.setBackground(background);
        } else {
            v.setBackgroundDrawable(background);
        }
    }

    /**
     * 保证在UI Thread中执行。
     */
    public static void addView(final ViewManager viewManager, final View view, final ViewGroup.LayoutParams params) {
        MsgMgr.getInstance().runOnUiThread(() -> {
            try {// 对activity并未开始创建时的添加行为进行错误拦截
                viewManager.addView(view, params);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }

    /**
     * 保证在UI Thread中执行。
     */
    public static void removeView(final ViewManager viewManager, final View view) {
        MsgMgr.getInstance().runOnUiThread(() -> viewManager.removeView(view));
    }

    /**
     * 在Main UIThread中设置ImageView的src。
     *
     * @param imageView ImageView实例。
     * @param resId     资源Id。
     */
    public static void setImageResource(final ImageView imageView, final int resId) {
        if (imageView == null) {
            return;
        }

        if (Looper.myLooper() == Looper.getMainLooper()) {
            imageView.setImageResource(resId);
            return;
        }

        MsgMgr.getInstance().runOnUiThread(() -> imageView.setImageResource(resId));
    }

    /**
     * 在Main UIThread中设置ImageView的src。
     *
     * @param imageView ImageView实例。
     * @param bm        图片资源。
     */
    public static void setImageBitmap(final ImageView imageView, final Bitmap bm) {
        if (imageView == null) {
            return;
        }
        if (Looper.myLooper() == Looper.getMainLooper()) {
            imageView.setImageBitmap(bm);
            return;
        }
        MsgMgr.getInstance().runOnUiThread(() -> imageView.setImageBitmap(bm));
    }

    /**
     * 通过文件名获取资源Id。
     */
    public static int getResIdFromDrawable(Context context, String fileName) {
        try {
            return context.getResources().getIdentifier(fileName, "drawable", context.getPackageName());
        } catch (Exception e) {
            PLogger.printThrowable(e);
        }

        return 0;
    }

    public static int getColorWithAlpha(float alpha, int baseColor) {
        int a = Math.min(255, Math.max(0, (int) (alpha * 255))) << 24;
        int rgb = 0x00ffffff & baseColor;
        return a + rgb;
    }
}
